
function pageload() {
	var now = new Date();
	var year = now.getFullYear(); // 年
	var month = now.getMonth() + 1; // 月
	var day = now.getDate(); // 日
	var clock = year + "-";
	if (month < 10)
		clock += "0";
	clock += month + "-";
	if (day < 10)
		clock += "0";
	clock += day + " ";
    var startTime = playForm.getField('startTime');
    var endTime = playForm.getField('endTime');
    startTime.set('value',clock + "00:00:00");
    endTime.set('value',clock + "23:59:59");
//	document.getElementById("startTime").value = clock + "00:00:00";
//	document.getElementById("endTime").value = clock + "23:59:59";
	$("#tree").html("");
	//$("#TextPwd")[0].focus();
    loginCMS();
}

///*****验证时间格式******/
//function strDateTime(str){
//	var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
//	var r = str.match(reg);
//	if (r == null)
//		return false;
//	var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
//	return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3]
//			&& d.getDate() == r[4] && d.getHours() == r[5]
//			&& d.getMinutes() == r[6] && d.getSeconds() == r[7]);
//}
//
//function checkDateTime(str) {
//	if (!strDateTime(str)) {
//		alert("您输入的时间格式不正确，请重新输入！\n正确的格式如：2012-01-01 00:00:00");
//	}
//}

/*****登录CMS******/
function loginCMS(){
	var OCXobj = document.getElementById("PlayBackOCX");
	//var ret=OCXobj.LoginCMS(userName,pw,ipAdd,port);
	var ret=OCXobj.Login_V11(username,password,ip,port,0);
	switch(ret){
		case 0:
			initTree();
			break;
		case -1:
            alert('视频管理参数配置有误！')
			break;
		default:
			break;
	}
}

function initTree(){
	var OCXobj = document.getElementById("PlayBackOCX");
	$("#tree").html("");
	//初始化中心节点
	var xmlStr = OCXobj.GetResourceInfo(1);
	var orderdCellIds = initControlUnitNodes(xmlStr);
	//初始化区域节点
	if(orderdCellIds.length > 0){
		xmlStr = OCXobj.GetResourceInfo(2);
		var regionIds = initRegionNodes(xmlStr, orderdCellIds);
		//初始化监控点节点
		if(regionIds.length > 0){
			xmlStr = OCXobj.GetResourceInfo(5);
			initCameraNodes(xmlStr, regionIds);
		}
	}


	$("#tree a").click(function(){
		//找到主菜单项对应的子菜单项
		$(this).next("ul").slideToggle();
		changeIcon($(this).parent("li"));
	});
}

/**
 * 初始化中心节点
 * @param {} xmlStr
 * @return {}
 */
function initControlUnitNodes(xmlStr){
	var xmldom=null;
	if(navigator.userAgent.toLowerCase().indexOf("msie")!=-1){
		xmldom = new ActiveXObject("Microsoft.XMLDOM");
		xmldom.loadXML(xmlStr);
	}else{
		xmldom = new DOMParser().parseFromString(xmlStr,"text/xml");
	}

	//CtrlCell的jQuery对象数组
	var ctrlCells = new Array();
	var orderdCellIds = new Array();
	$(xmldom).find("CtrlCell").each(function(){
		ctrlCells.push($(this));
	});
	if(ctrlCells.length > 0){
		var rootUnit = ctrlCells[0];

		var curLevelCellIds = new Array();
		var parentCellId = 0;
		var curCellId = 0;
		curLevelCellIds.push($(rootUnit).find("HighCtrlCellId").text());
		//循环到ctrlCells中不再有任何元素
		while(ctrlCells.length>0){
			//本次for循环查找所用parentCellId
			parentCellId = curLevelCellIds.pop();

			//循环查找父中心Id=parentCellId的CtrlCell
			for(var i=0; i<ctrlCells.length; ){
				if($(ctrlCells[i]).find("HighCtrlCellId").text()==parentCellId){
					//子中心Id入栈，下次for循环查找时作为parentCellId
					curCellId = $(ctrlCells[i]).find("CtrlCellId").text();
					curLevelCellIds.push(curCellId);
					orderdCellIds.push(curCellId);
					//添加子中心节点
					if($("#tree").html()==""){
						$("#tree").html("<li class='control_unit_li'><a style='text-decoration: none;color: black;margin-left: 30px;' href='#'>"+$(ctrlCells[i]).find("CtrlCellName").text()+"</a><ul id='ul_cell_" + curCellId + "'></ul></li>");
					}else{
						var expr = "#ul_cell_"+parentCellId;
						$(expr).append("<li class='control_unit_li'><a style='text-decoration: none;color: black;margin-left: 30px;' href='#'>"+$(ctrlCells[i]).find("CtrlCellName").text()+"</a><ul id='ul_cell_" + curCellId + "'></ul></li>");
					}
					//从CtrlCell数组中删除已添加的CtrlCell
					ctrlCells.splice(i,1);
				}else{
					i++;
				}
			}
		}
	}
	return orderdCellIds;
}

/**
 * 将区域节点挂到各中心下
 * @param {} xmlStr
 * @param {} orderdCellIds
 * @return {}
 */
function initRegionNodes(xmlStr, orderdCellIds){
	var xmldom=null;
	if(navigator.userAgent.toLowerCase().indexOf("msie")!=-1){
		xmldom = new ActiveXObject("Microsoft.XMLDOM");
		xmldom.loadXML(xmlStr);
	}else{
		xmldom = new DOMParser().parseFromString(xmlStr,"text/xml");
	}

	//Region的jQuery对象数组
	var regionIds = new Array();
	var regionsOff = new Array();
	var expr;
	for(var i=0; i<orderdCellIds.length; i++){
		var ctrlCellId = orderdCellIds[i];
		//查找中心下所有的区域
		$(xmldom).find("Region").each(function(){
			if($(this).find("CtrlCellId").text() == ctrlCellId){
				if($(this).find("HighRegionId").text() == 0){
					expr = "#ul_cell_" + ctrlCellId;
					$(expr).append("<li class='region_li'><a style='text-decoration: none;color: black;margin-left: 30px;' href='#'>"+$(this).find("RegionName").text()+"</a><ul id='ul_region_"+$(this).find("RegionId").text()+"'></ul></li>");
					regionIds.push($(this).find("RegionId").text());
				}else{
					regionsOff.push(this);
				}
			}
		});

		while(regionsOff.length > 0){
			for(var j=0; j<regionsOff.length; ){
				var regionNode = regionsOff[j];
				expr = "#ul_region_" + $(regionNode).find("HighRegionId").text();
				if($(expr).length > 0){
					$(expr).append("<li class='region_li'><a style='text-decoration: none;color: black;margin-left: 30px;' href='#'>"+$(regionNode).find("RegionName").text()+"</a><ul id='ul_region_"+$(regionNode).find("RegionId").text()+"'></ul></li>");
					regionsOff.splice(j,1);
					regionIds.push($(regionNode).find("RegionId").text());
				}else{
					j++;
				}
			}
		}
	}

	return regionIds;
}

/**
 * 将监控点节点挂到各区域下
 * @param {} xmlStr
 * @param {} regionIds
 */
function initCameraNodes(xmlStr, regionIds){
	var xmldom=null;
	if(navigator.userAgent.toLowerCase().indexOf("msie")!=-1){
		xmldom = new ActiveXObject("Microsoft.XMLDOM");
		xmldom.loadXML(xmlStr);
	}else{
		xmldom = new DOMParser().parseFromString(xmlStr,"text/xml");
	}

	var expr;
	var cameras = $(xmldom).find("Camera");
	for(var i=0; i<cameras.length; i++){
		var cameraId = $(cameras[i]).find("CameraId").text();
		expr = "#ul_region_" + $(cameras[i]).find("RegionId").text();
		$(expr).append("<li class='camera_li'><a style='text-decoration: none;color: black;margin-left: 30px;' href='#'>"+$(cameras[i]).find("CameraName").text()+"</a><ul id='ul_camera_"+cameraId+"'></ul></li>");
		var iReclocation;
		var recLocations = $(cameras[i]).find("RecLocation");
		expr = "#ul_camera_" + cameraId;
		for(var l=0; l<recLocations.length; l++){
			switch($(recLocations[l]).text()){
				case "IPSAN":
					iReclocation = 0;break;
				case "设备":
					iReclocation = 1;break;
				case "PCNVR":
					iReclocation = 2;break;
				case "NVR":
					iReclocation = 3;break;
				case "CVR":
					iReclocation = 7;break;
			}
			$(expr).append("<li ondblclick='SetPlayBackParam("+cameraId+","+iReclocation+");' class='recloaction'><a style='text-decoration: none;color: black;' href='#'>"+$(recLocations[l]).text()+"</a></li>");
		}
	}
}

/*****设置本地参数******/
function SetlocalParam() {
	var OCXobj = document.getElementById("PlayBackOCX");
	var PicType = document.getElementById("SelectPicType").value;
	var PicPath = document.getElementById("TextPicPath").value;
	var RecordPath = document.getElementById("TextRecordPath").value;
	//if(PicPath=="" || RecordPath==""){
	//	alert("保存路径不能为空！");
	//	return;
	//}
	strXML = "<?xml version='1.0'?><Parament><CappicMode>" + PicType
			+ "</CappicMode><CappicPath>" + PicPath + "</CappicPath><CutPath>"
			+ RecordPath + "</CutPath><CutFileSize>2</CutFileSize></Parament>";
	var ret = OCXobj.SetLocalParam(strXML);
	switch (ret) {
	case 0:
		//alert("设置成功！");
//		showMethodInvokedInfo("SetLocalParam接口调用成功！");
		break;
	case -1:
		//alert("设置失败！");
//		showMethodInvokedInfo("SetLocalParam接口调用失败！错误码："
//				+ OCXobj.GetLastError_V11());
		break;
	default:
		break;
	}
}

/** ***设置回放参数***** */
function SetPlayBackParam(CameraID, RecLocation) {
	var OCXobj = document.getElementById("PlayBackOCX");
	var WndIndex = OCXobj.GetFocusWndIndex();// 获取当前选中窗口
	var ret = OCXobj.SetPlayBackParam_V11(CameraID, RecLocation, WndIndex);
	switch (ret) {
	case 0:
		//alert("设置成功！");
		//showMethodInvokedInfo("SetPlayBackParam_V11接口调用成功！");
		break;
	case -1:
		//alert("设置失败！");
		//showMethodInvokedInfo("SetPlayBackParam_V11接口调用失败！错误码："
//				+ OCXobj.GetLastError_V11());
		break;
	default:
		break;
	}
}

/** ***搜索录像***** */
function StartQueryRecord() {
	var OCXobj = document.getElementById("PlayBackOCX");
	StartTime = document.getElementById("startTime").value;
	EndTime = document.getElementById("endTime").value;
	RecType = document.getElementsByName("TextRecType");
	var CheRecType = 0;
	for (i = 0; i < RecType.length; i++) {
		if (RecType[i].checked == true) {
			CheRecType += parseInt(RecType[i].value);
		}
	}
	if ("0" == CheRecType) {
		alert("录像类型不能为空！");
		return;
	}
	var ret = OCXobj.StartQueryRecord_V11(StartTime, EndTime, CheRecType);
	switch (ret) {
	case 0:
        //alert("成功");
		//showMethodInvokedInfo("StartQueryRecord_V11接口调用成功！");
		break;
	case -1:
		alert("搜索失败！");
//		showMethodInvokedInfo("StartQueryRecord_V11接口调用失败！错误码："
//				+ OCXobj.GetLastError_V11());
		break;
	default:
		break;
	}
}

function changeIcon(mainNode) {
	if (mainNode) {
		if (mainNode.css("background-image").indexOf("Control_area_exp.png") >= 0) {
			mainNode.css("background-image","url('/resources/video/images/Control_area_col.png')");
		} else if (mainNode.css("background-image").indexOf("Control_area_col.png") >= 0) {
			mainNode.css("background-image","url('/resources/video/images/Control_area_exp.png')");
		} else if (mainNode.css("background-image").indexOf("Area_exp.png") >= 0) {
			mainNode.css("background-image","url('/resources/video/images/Area_col.png')");
		} else if (mainNode.css("background-image").indexOf("Area_col.png") >= 0) {
			mainNode.css("background-image","url('/resources/video/images/Area_exp.png')");
		} else if (mainNode.css("background-image").indexOf("Camera_exp.png") >= 0) {
			mainNode.css("background-image","url('/resources/video/images/Camera_col.png')");
		} else if (mainNode.css("background-image").indexOf("Camera_col.png") >= 0) {
			mainNode.css("background-image","url('/resources/video/images/Camera_exp.png')");
		}
	}
}